<?php
namespace jy_quanhuo;
defined('BY_JYA') or exit('error');
/*
*/
class member extends \plugin{
	/*
		核实身份
	*/
	public function check_up($pid=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$tpl=pdo_get('jy_merch_game_item_tpl',array('id'=>$pid),array('id','title','gamename'));
		$user=pdo_get('jy_merch_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$check=model('jy_merch/games')->checkup($user['id'],$tpl['gamename']);
		if(!$check){
			exi('','','jy_quanhuo/open/');
		}
		return true;
	}
	public function up_fromuser($pid=0,$mid=0,$fromuser=0,$merchid=0,$become_child=0){
		$setting=$this->plugin_setting('basic');
		$mid=$mid?:$_SESSION['uid'];
		$fromuser=$fromuser?:$_SESSION['fromuser'];
		if(!$mid){
			return false;
		}
		//只有直接访问才要上级ID
		if(!$fromuser && !$setting['become_child']){
			return false;
		}
		if(!$setting['become_type']){
			$pid=0;
		}
		//进来的场景，跟设置的不匹配
		if($setting['become_child'] && $setting['become_child']!=$become_child){
			$isup=pdo_get('jy_merch_game_fromuser',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$pid,'mid'=>$mid));
			if($isup['id']){
				if($fromuser!=$isup['fz_fromuser']){
					pdo_update('jy_merch_game_fromuser',array('fz_fromuser'=>$fromuser),array('uniacid'=>$_SESSION['uniacid'],'pid'=>$pid,'mid'=>$mid));
				}
				return false;
			}
			//暂时存上级
			$indata=array(
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$merchid,
				'pid'=>$pid,
				'mid'=>$mid,
				'fz_fromuser'=>$fromuser,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_merch_game_fromuser',$indata);
			return false;
		}
		$isup=pdo_get('jy_merch_game_fromuser',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$pid,'mid'=>$mid));
		if($isup['id']){
			//激活关系
			if(!$isup['fromuser']){
				pdo_update('jy_merch_game_fromuser',array('fromuser'=>$isup['fz_fromuser']),array('uniacid'=>$_SESSION['uniacid'],'pid'=>$pid,'mid'=>$mid));
			}
			return false;
		}
		if(!$fromuser){
			return false;
		}
		$indata=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$merchid,
			'pid'=>$pid,
			'mid'=>$mid,
			'fromuser'=>$fromuser,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_merch_game_fromuser',$indata);
		if(!$setting['mian_type']){
			$this->model('order')->mianup($pid,$fromuser);
		}
		return true;
	}
}